using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;
using System.Data.SqlClient;
using System.Configuration;


namespace SIS.BLL
{

    /// <summary>
    /// The TeacherProfileTableManager class is responsible for managing BO.TeacherProfileTable objects in the system.
    /// </summary>
    [DataObjectAttribute()]
    public class TeacherProfileTableManager
    {

        #region "Public Methods"

        /// <summary>
        /// Gets a list with all TeacherProfileTable objects in the database.
        /// </summary>
        /// <returns>A list with all TeacherProfileTable from the database when the database contains any, or null otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static TeacherProfileTableList GetList(int SchoolKey)
        {
            return TeacherProfileTableDB.GetList(SchoolKey);
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static TeacherProfileTableList GetList(int SchoolKey, string Name, String Address, String NRICNO, Int16 AddressType, Int16 CountryCode, int CityCode, int DistrictCode)
        {
            return TeacherProfileTableDB.GetList(SchoolKey, Name, Address, NRICNO, AddressType, CountryCode, CityCode, DistrictCode);
        }

        /// <summary>
        /// Gets a single TeacherProfileTable from the database without its data.
        /// </summary>
        /// <param name="teacherProfileKey">The TeacherProfileKey of the TeacherProfileTable in the database.</param>
        /// <returns>A TeacherProfileTable object when the TeacherProfileKey exists in the database, or <see langword="null"/> otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static TeacherProfileTable GetItem(int teacherProfileKey, int SchoolKey)
        {
            return GetItem(teacherProfileKey, SchoolKey, false);
        }

        /// <summary>
        /// Gets a single TeacherProfileTable from the database.
        /// </summary>
        /// <param name="teacherProfileKey">The TeacherProfileKey of the TeacherProfileTable in the database.</param>
        /// <param name="getTeacherProfileTableRecords">Determines whether to load all associated TeacherProfileTable records as well.</param>
        /// <returns>
        /// A TeacherProfileTable object when the TeacherProfileKey exists in the database, or <see langword="null"/> otherwise.
        /// </returns>
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static TeacherProfileTable GetItem(int teacherProfileKey, int SchoolKey, bool getTeacherProfileTableRecords)
        {
            TeacherProfileTable myTeacherProfileTable = TeacherProfileTableDB.GetItem(teacherProfileKey, SchoolKey);
            if (myTeacherProfileTable != null && getTeacherProfileTableRecords)
            {
                myTeacherProfileTable.timeScheduleTables = TimeScheduleTableDB.GetListByTeacherProfileKey(teacherProfileKey);
            }
            return myTeacherProfileTable;
        }

        /// <summary>
        /// Saves a TeacherProfileTable in the database.
        /// </summary>
        /// <param name="myTeacherProfileTable">The TeacherProfileTable instance to save.</param>
        /// <returns>The new TeacherProfileKey if the TeacherProfileTable is new in the database or the existing TeacherProfileKey when an item was updated.</returns>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public static int Save(TeacherProfileTable myTeacherProfileTable)
        {
            using (TransactionScope myTransactionScope = new TransactionScope())
            {
                int teacherProfileTableTeacherProfileKey = TeacherProfileTableDB.Save(myTeacherProfileTable);
                foreach (TimeScheduleTable myTimeScheduleTable in myTeacherProfileTable.timeScheduleTables)
                {
                    myTimeScheduleTable.TeacherProfileKey = teacherProfileTableTeacherProfileKey;
                    TimeScheduleTableDB.Save(myTimeScheduleTable);
                }

                //  Assign the TeacherProfileTable its new (or existing TeacherProfileKey).
                myTeacherProfileTable.TeacherProfileKey = teacherProfileTableTeacherProfileKey;

                myTransactionScope.Complete();

                return teacherProfileTableTeacherProfileKey;
            }
        }

        /// <summary>
        /// Deletes a TeacherProfileTable from the database.
        /// </summary>
        /// <param name="myTeacherProfileTable">The TeacherProfileTable instance to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(TeacherProfileTable myTeacherProfileTable)
        {


            return TeacherProfileTableDB.Delete(myTeacherProfileTable.TeacherProfileKey);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(int TeacherProfileKey, int SchoolKey)
        {
            SqlTransaction myTransaction = null;
            bool lResult = true;
            int count = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                try
                {
                    myConnection.Open();
                    myTransaction = myConnection.BeginTransaction();
                    TeacherProfileTable objProfile = new TeacherProfileTable();
                    objProfile = TeacherProfileTableManager.GetItem(TeacherProfileKey, SchoolKey);
                    TeacherQualtificationTableList objTeacherQualification = new TeacherQualtificationTableList();
                    TeacherOtherSkillsTableList objTeacherOtherSkill = new TeacherOtherSkillsTableList();
                    TeacherJobExperiencesTableList objTeacherJobExp = new TeacherJobExperiencesTableList();
                    TeacherDesignationTableList objTeacherDesignation = new TeacherDesignationTableList();
                    TeacherSalaryTableList objTeacherSalary = new TeacherSalaryTableList();

                    int UserAccountKey = 0;
                    if (objProfile != null)
                    {
                        UserAccountKey = objProfile.UserAccountKey;

                    }
                    lResult = TeacherProfileTableDB.Delete(TeacherProfileKey);
                    objTeacherDesignation = TeacherDesignationTableManager.GetList(UserAccountKey);
                    foreach (TeacherDesignationTable item in objTeacherDesignation)
                    {
                        if (TeacherDesignationTableManager.Delete(item.TeacherDesignationKey))
                        {
                            count = count + 1;
                        }
                    }
                    count = 0;
                    objTeacherSalary = TeacherSalaryTableManager.GetList(UserAccountKey);
                    foreach (TeacherSalaryTable item in objTeacherSalary)
                    {
                        if (TeacherSalaryTableManager.Delete(item.TeacherSalaryKey))
                        {
                            count = count + 1;
                        }
                    }
                    count = 0;
                    objTeacherQualification = TeacherQualtificationTableManager.GetList(UserAccountKey);
                    foreach (TeacherQualtificationTable item in objTeacherQualification)
                    {
                        if (TeacherQualtificationTableManager.Delete(item.TeacherQualtificationKey))
                        {
                            count = count + 1;
                        }
                    }
                    count = 0;
                    objTeacherOtherSkill = TeacherOtherSkillsTableManager.GetList(UserAccountKey);
                    foreach (TeacherOtherSkillsTable item in objTeacherOtherSkill)
                    {
                        if (TeacherOtherSkillsTableManager.Delete(item.TeacherOtherSkillsKey))
                        {
                            count = count + 1;
                        }
                    }
                    count = 0;
                    objTeacherJobExp = TeacherJobExperiencesTableManager.GetList(UserAccountKey);
                    foreach (TeacherJobExperiencesTable item in objTeacherJobExp)
                    {
                        if (TeacherJobExperiencesTableManager.Delete(item.TeacherJobExperienceKey))
                        {
                            count = count + 1;
                        }
                    }
                    myTransaction.Commit();

                }

                catch (Exception ex)
                {
                    myTransaction.Rollback();
                    throw ex;
                }
                finally
                {
                    myConnection.Close();
                }
            }

            return lResult;

        }


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static TeacherProfileTable GetItemByUserAccountKey(int UserAccountKey, int SchoolKey)
        {
            TeacherProfileTable myStudentProfileTable = TeacherProfileTableDB.GetItemByUserAccountKey(UserAccountKey, SchoolKey);
            return myStudentProfileTable;
        }

        public static bool UpdatePhoto(int TeacherProfileKey, Byte[] imgPhoto)
        {
            return TeacherProfileTableDB.UpdatePhoto(TeacherProfileKey, imgPhoto);
        }

        public static Byte[] GetPhoto(int TeacherProfileKey)
        {
            return TeacherProfileTableDB.GetPhoto(TeacherProfileKey);
        }

        #endregion

    }

}
